<?php

require_once(I('server.DOCUMENT_ROOT') . '/GETUISDK/' . 'IGt.Push.php');
require_once(I('server.DOCUMENT_ROOT') . '/GETUISDK/' . 'igetui/IGt.AppMessage.php');
require_once(I('server.DOCUMENT_ROOT') . '/GETUISDK/' . 'igetui/IGt.APNPayload.php');
require_once(I('server.DOCUMENT_ROOT') . '/GETUISDK/' . 'igetui/template/IGt.BaseTemplate.php');
require_once(I('server.DOCUMENT_ROOT') . '/GETUISDK/' . 'IGt.Batch.php');
require_once(I('server.DOCUMENT_ROOT') . '/GETUISDK/' . 'igetui/utils/AppConditions.php');

//http的域名
define('HOST','http://sdk.open.api.igexin.com/apiex.htm');

//https的域名
//define('HOST','https://api.getui.com/apiex.htm');
               

define('APPKEY','ZNDA7YjNWUAwy5Ih0uGtX');
define('APPID','RLE401A5Dx747oNj8zm2t9');
define('MASTERSECRET','STIPX6TE6499cwPCdadX56');



function PushSingleDeviceIOS($cid,$title='',$content='',$trans=array())
{
		$igt = new \IGeTui(HOST,APPKEY,MASTERSECRET);
		$trans['title']=$title;
    	$trans['content']=$content;
    	//$trans=json_encode($trans);
    	//$trans=implode(",",$trans);

    	//$rep = $igt->getClientIdStatus(APPID,CID);
    	$state = $igt->getClientIdStatus(APPID,$cid);
    	// if($state['result']=='Offline'){
    		$template = IGtTransmissionTemplateDemo($title,$content,$trans);
    	// }else{
    		// $template = IGtNotificationTemplateDemo($title,$content,$trans);
    	// }
		
	  	
	    //个推信息体
	    $message = new \IGtSingleMessage();
	    $message->set_isOffline(true);//是否离线
	    $message->set_offlineExpireTime(3600*12*1000);//离线时间
	    $message->set_data($template);//设置推送消息类型
	//	$message->set_PushNetWorkType(0);//设置是否根据WIFI推送消息，1为wifi推送，0为不限制推送
	    //接收方
	    $target = new \IGtTarget();
	    $target->set_appId(APPID);
	    $target->set_clientId($cid);
	    //$target->set_alias($alias);

	    try {
	        $rep = $igt->pushMessageToSingle($message, $target);
	        return $rep;
	    }catch(RequestException $e){
	        $requstId =$e.getRequestId();
	        $rep = $igt->pushMessageToSingle($message, $target,$requstId);
	        return $rep;
	    }
}

function PushDeviceListMultipleNotificationIOS($accountList,$title='',$content='',$trans=array())
{
	putenv("gexin_pushList_needDetails=true");
    putenv("gexin_pushList_needAsync=true");
    $trans['title']=$title;
    $trans['content']=$content;
    //$trans=json_encode($trans);
    	//$trans=implode(",",$trans);
    
    $igt = new \IGeTui(HOST, APPKEY, MASTERSECRET);

    $template = IGtTransmissionTemplateDemo($title,$trans);

    //个推信息体
    $message = new \IGtListMessage();
    $message->set_isOffline(true);//是否离线
    $message->set_offlineExpireTime(3600 * 12 * 1000);//离线时间
    $message->set_data($template);//设置推送消息类型
//    $message->set_PushNetWorkType(1);	//设置是否根据WIFI推送消息，1为wifi推送，0为不限制推送
//    $contentId = $igt->getContentId($message);
    $contentId = $igt->getContentId($message,$trans);	//根据TaskId设置组名，支持下划线，中文，英文，数字


    //接收方1
    
    foreach($accountList as $v){
    	$target1 = new \IGtTarget();
	    $target1->set_appId(APPID);
	    $target1->set_clientId($v);
	    $targetList[] = $target1;
	}
    $rep = $igt->pushMessageToList($contentId, $targetList);
	return $rep;
}


function IGtTransmissionTemplateDemo($title,$content,$trans){
	//$trans=stripslashes($trans);

    $template =  new \IGtTransmissionTemplate();
    $template->set_appId(APPID);//应用appid
    $template->set_appkey(APPKEY);//应用appkey
    //$template->set_transmissionType(2);//透传消息类型
    //$template->set_transmissionContent(json_encode($trans));//透传内容
    //$template->set_duration(BEGINTIME,ENDTIME); //设置ANDROID客户端在此时间区间内展示消息
    //APN简单推送
       /*$template = new \IGtAPNTemplate();
       $apn = new \IGtAPNPayload();
       $alertmsg=new \SimpleAlertMsg();
       $alertmsg->alertMsg=$title;
       $apn->alertMsg=$alertmsg;
//        $apn->badge=2;
//        $apn->sound="";

       print_r($trans);
       $apn->add_customMsg("payload",json_encode($trans));
       //$apn->contentAvailable=1;
       $apn->category="ACTIONABLE";
       $template->set_apnInfo($apn);
       $message = new \IGtSingleMessage();*/

    //APN高级推送
    $apn = new \IGtAPNPayload();
    $alertmsg=new \DictionaryAlertMsg();
    $alertmsg->body="body";
    $alertmsg->actionLocKey="ActionLockey";
    $alertmsg->locKey=$content;
    $alertmsg->locArgs="ddd";
    $alertmsg->launchImage="launchimage";
//        IOS8.2 支持
    $alertmsg->title=$title;
    $alertmsg->titleLocKey='aaaaa';
    $alertmsg->titleLocArgs='bbbbbb';

    //$apn->alertMsg=$alertmsg;
    $apn->badge=7;
    $apn->sound="default";
    $apn->add_customMsg("payload",json_encode($trans));
    $apn->contentAvailable=1;
    $apn->category="ACTIONABLE";
    $template->set_apnInfo($apn);

    //PushApn老方式传参
//    $template = new IGtAPNTemplate();
//          $template->set_pushInfo("", 10, "", "com.gexin.ios.silence", "", "", "", "");

    return $template;
}

 function IGtNotificationTemplateDemo($title,$content,$trans){
	    $template =  new \IGtNotificationTemplate();
	    $template->set_appId(APPID);//应用appid
	    $template->set_appkey(APPKEY);//应用appkey
	    $template->set_transmissionType(2);//透传消息类型
	    $template->set_transmissionContent(json_encode($trans));//透传内容
	    /*$template->set_title($title);//通知栏标题
	    $template->set_text($content);//通知栏内容
	    $template->set_logo("http://www.appwuhan.net:10085/jianwei.png");//通知栏logo
	    $template->set_isRing(true);//是否响铃
	    $template->set_isVibrate(true);//是否震动
	    $template->set_isClearable(true);//通知栏是否可清除*/
	    //$template->set_duration(BEGINTIME,ENDTIME); //设置ANDROID客户端在此时间区间内展示消息
	    //iOS推送需要设置的pushInfo字段
	       $apn = new \IGtAPNPayload();
		    $alertmsg=new \DictionaryAlertMsg();
		    $alertmsg->body="body";
		    $alertmsg->actionLocKey="ActionLockey";
		    $alertmsg->locKey=$content;
		    $alertmsg->locArgs=$trans;
		    $alertmsg->launchImage="launchimage";
		//        IOS8.2 支持
		    $alertmsg->title=$title;
		    $alertmsg->titleLocKey=$title;
		    $alertmsg->titleLocArgs=$trans;

		    $apn->alertMsg=$alertmsg;
		    $apn->badge=7;
		    $apn->sound="";
		    $apn->add_customMsg("payload",json_encode($trans));
		    $apn->contentAvailable=1;
		    $apn->category="ACTIONABLE";
		    $template->set_apnInfo($apn);
	    return $template;
	}



	function pushAPN($device,$title,$content,$trans){

    //APN简单推送
        /*$igt = new \IGeTui(HOST,APPKEY,MASTERSECRET);
        $template = new \IGtAPNTemplate();
        $apn = new \IGtAPNPayload();
        $alertmsg=new \SimpleAlertMsg();
        $alertmsg->alertMsg="";
        $apn->alertMsg=$alertmsg;
//        $apn->badge=2;
        $apn->sound="";
        $apn->add_customMsg("payload",json_encode($trans));
        $apn->contentAvailable=1;
        $apn->category="ACTIONABLE";
        $template->set_apnInfo($apn);
        $message = new \IGtSingleMessage();*/

    //APN高级推送
    $trans['title']=$title;
    $trans['content']=$content;
       $igt = new \IGeTui(HOST,APPKEY,MASTERSECRET);
       $template = new \IGtAPNTemplate();
       $apn = new \IGtAPNPayload();
       $alertmsg=new \DictionaryAlertMsg();
       $alertmsg->body=$content;
       $alertmsg->actionLocKey="ActionLockey";
       //$alertmsg->locKey="LocKey";
       //$alertmsg->locArgs=array("locargs");
       //$alertmsg->launchImage="launchimage";
//        IOS8.2 支持
       $alertmsg->title=$title;
       //$alertmsg->titleLocKey="TitleLocKey";
       //$alertmsg->titleLocArgs=array("TitleLocArg");

       $apn->alertMsg=$alertmsg;
       //$apn->badge=7;
       //$apn->sound="test1.wav";
       $apn->add_customMsg("payload",json_encode($trans));
       //$apn->contentAvailable=1;
       $apn->category="ACTIONABLE";
       $template->set_apnInfo($apn);
       $message = new \IGtSingleMessage();

    //PushApn老方式传参
//    $igt = new IGeTui(HOST,APPKEY,MASTERSECRET);
//    $template = new IGtAPNTemplate();
//    $template->set_pushInfo("actionLocKey", 6, "body", "", "payload", "locKey", "locArgs", "launchImage",1);
//    $message = new IGtSingleMessage();
////
    $message->set_data($template);
   $ret = $igt->pushAPNMessageToSingle(APPID, $device, $message);
    return $ret;
}

/*function PushAPNListMultipleNotificationIOS($accountList,$title='',$content='',$trans=array()){
  $ret=array();
	foreach($accountList as $v){
		$ret[]=pushAPN($v,$title,$content,$trans);
	}
	return $ret;
}*/

function PushAPNListMultipleNotificationIOS($accountList,$title='',$content='',$trans=array()){
  $trans['title']=$title;
    $trans['content']=$content;
       $igt = new \IGeTui(HOST,APPKEY,MASTERSECRET);
       $template = new \IGtAPNTemplate();
       $apn = new \IGtAPNPayload();
       $alertmsg=new \DictionaryAlertMsg();
       $alertmsg->body=$content;
       $alertmsg->actionLocKey="ActionLockey";
       //$alertmsg->locKey="LocKey";
       //$alertmsg->locArgs=array("locargs");
       //$alertmsg->launchImage="launchimage";
//        IOS8.2 支持
       $alertmsg->title=$title;
       //$alertmsg->titleLocKey="TitleLocKey";
       //$alertmsg->titleLocArgs=array("TitleLocArg");

       $apn->alertMsg=$alertmsg;
       //$apn->badge=7;
       //$apn->sound="test1.wav";
       $apn->add_customMsg("payload",json_encode($trans));
       //$apn->contentAvailable=1;
       $apn->category="ACTIONABLE";
       $template->set_apnInfo($apn);


    //PushApn老方式传参
//    $igt = new IGeTui(HOST,APPKEY,MASTERSECRET);
//    $template = new IGtAPNTemplate();
//    $template->set_pushInfo("actionLocKey", 6, "body", "", "payload", "locKey", "locArgs", "launchImage",1);
//    $message = new IGtSingleMessage();
////
   //多个用户推送接口
    putenv("needDetails=true");
    $listmessage = new IGtListMessage();
    $listmessage->set_data($template);
    $contentId = $igt->getAPNContentId(APPID, $listmessage);
    //$deviceTokenList = array("3337de7aa297065657c087a041d28b3c90c9ed51bdc37c58e8d13ced523f5f5f");
    $deviceTokenList = $accountList;
    $ret = $igt->pushAPNMessageToList(APPID, $contentId, $deviceTokenList);
}

